package com.zt.springcloud.controller;

import cloud.zt.springboot.utils.StringUtils;
import com.zt.springcloud.entity.Address;
import com.zt.springcloud.entity.Role;
import com.zt.springcloud.entity.User;
import com.zt.springcloud.service.RoleService;
import com.zt.springcloud.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author RedReins
 * @version 1.0
 * @description: TODO
 * @date 2021/8/28 18:00
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    /**
     * 获取当前用户信息
     */
    @GetMapping("/info/{username}")
    public User info(@PathVariable("username") String username)
    {
        log.info("SystemModules:username:{}",username);
        User user = userService.selectUserByUserName(username);

        if (StringUtils.isNull(user))
        {
            return null;
        }
        // 角色集合
        Set<Role> roles = roleService.getRolePermission(user.getId());
        return user;
    }

    /**
     * 获取当前用户角色
     */
    @GetMapping("/roles/{id}")
    public Set<Role> getRolesInfo(@PathVariable("id") Long id)
    {
        Set<Role> roles = roleService.getRolePermission(id);
        if (StringUtils.isNull(roles))
        {
            return null;
        }

        return roles;
    }

    /*
     * 添加用户地址
     * */
    @RequestMapping(value = "/addAddress",method = RequestMethod.POST)
    public Integer addUserAddress(@RequestBody(required = false) Address address){
        return userService.addUserAddress(address);
    }

    /*
    * 查询用户地址
    * */
    @RequestMapping(value = "/getUserAddress",method = RequestMethod.GET)
    public List<Address> getAddressInfo(@RequestParam("userid") Long userid){
        return userService.getAddressInfo(userid);
    }

    /*
     * 更新用户地址
     * */
    @RequestMapping(value = "/updateAddress",method = RequestMethod.POST)
    public Integer updateAddressInfo(@RequestBody(required = false) Address address){
        System.out.println(address.toString());
        return userService.updateAddressInfo(address);
    }

}
